/* 
 * File:   ComplexConverter.cpp
 * Author: Gab'z
 * 
 * Created on 4 juin 2012, 09:42
 */

#include "ComplexConverter.h"
#include "../../../Context.h"
#include "../../literal/Complex.h"
#include "../../literal/SimpleNumber.h"

namespace calculator {

    ComplexConverter::ComplexConverter(const std::string name) : UnaryOperator(name) {
    }

    ComplexConverter::ComplexConverter(const ComplexConverter& orig) : UnaryOperator(orig._name) {
    }

    ComplexConverter::~ComplexConverter() {
    }

    std::string ComplexConverter::isExecutable() const {
        if (Context::getInstance()->getCurrentStack()->size() == 0) return "Empty stack";
        return "";
    }

    ComplexConverter* ComplexConverter::clone() const {
        return new ComplexConverter(_name);
    }

    const Number* ComplexConverter::apply(const Number* number) const throw (ArithmeticException) {
        const Complex* c = dynamic_cast<const Complex*> (number);
        if (c != 0) {
            return new Complex(*c);
        }
        const SimpleNumber* sn = dynamic_cast<const SimpleNumber*> (number);
        if (sn != 0) {
            return new Complex(*sn);
        }
        throw ArithmeticException("NaN");
    }

}